import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.taobao.api.ApiException;
import org.jeecg.modules.api.OverrideCheck;
import org.jeecg.modules.config.Constant;
import org.jeecg.modules.config.URLConstant;
import org.jeecg.modules.controller.CallbackController;
import org.jeecg.modules.model.ProcessInstanceInputVO;
import org.jeecg.modules.smartStudent.entity.SmartStudentTeacherInfomation;
import org.jeecg.modules.smartStudent.mapper.SmartStudentTeacherInfomationMapper;
import org.jeecg.modules.util.AccessTokenUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ProcessOapiProcessinstanceCreateRequest2.class)
public class ProcessOapiProcessinstanceCreateRequest2 {
    @Resource
    private SmartStudentTeacherInfomationMapper smartStudentTeacherInfomationMapper;

    @Test
    public void testStartProcess() throws ApiException {
        ProcessInstanceInputVO processInstance = new ProcessInstanceInputVO();
        processInstance.setOriginatorUserId("266630304324369438");
        processInstance.setDeptId(1L);
        ProcessInstanceInputVO.TextForm textForms1 = new ProcessInstanceInputVO.TextForm();
        /*textForms1.setName("[\"开始时间\",\"结束时间\"]");
        textForms1.setValue("[\"2019-02-19\",\"2019-02-25\"]");*/
        textForms1.setName("姓名");
        textForms1.setValue("张三");
        ProcessInstanceInputVO.TextForm textForm2 = new ProcessInstanceInputVO.TextForm();
        textForm2.setName("性别");
        textForm2.setValue("男");
        ProcessInstanceInputVO.TextForm textForm3 = new ProcessInstanceInputVO.TextForm();
        textForm3.setName("出生年月");
        textForm3.setValue("2020-05-29");
        ProcessInstanceInputVO.TextForm textForm4 = new ProcessInstanceInputVO.TextForm();
        textForm4.setName("毕业院校");
        textForm4.setValue("太原工业学院");
        ProcessInstanceInputVO.TextForm textForm5 = new ProcessInstanceInputVO.TextForm();
        textForm5.setName("手机");
        textForm5.setValue("123456789");
        ProcessInstanceInputVO.TextForm textForm6 = new ProcessInstanceInputVO.TextForm();
        textForm6.setName("本人支持或参加过的典型项目");
        textForm6.setValue("本人支持或参加过的典型项目，本人支持或参加过的典型项目，本人支持或参加过的典型项目");
        ProcessInstanceInputVO.TextForm textForm7 = new ProcessInstanceInputVO.TextForm();
        textForm7.setName("照片");
        textForm7.setValue("[\"https://ae01.alicdn.com/kf/HTB1iww9aEGF3KVjSZFo762mpFXat.png\"]");
        ArrayList<ProcessInstanceInputVO.TextForm> textForms = new ArrayList<>();
        textForms.add(textForms1);
        textForms.add(textForm2);
        textForms.add(textForm3);
        textForms.add(textForm4);
        textForms.add(textForm5);
        textForms.add(textForm6);
        textForms.add(textForm7);
        processInstance.setTextForms(textForms);
        DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_PROCESSINSTANCE_START);
        OapiProcessinstanceCreateRequest request = new OapiProcessinstanceCreateRequest();
        request.setProcessCode(Constant.PROCESS_CODE);

        request.setFormComponentValues(processInstance.generateForms());

        request.setOriginatorUserId(processInstance.getOriginatorUserId());
        request.setDeptId(processInstance.getDeptId());

        OapiProcessinstanceCreateResponse response = client.execute(request, AccessTokenUtil.getToken());
        System.out.println(response);
    }


    @Test
    public void testStartProcess2() throws ApiException {
        ProcessInstanceInputVO processInstance = new ProcessInstanceInputVO();
        processInstance.setOriginatorUserId("266630304324369438");
        processInstance.setDeptId(1L);
        ProcessInstanceInputVO.TextForm textForm1 = new ProcessInstanceInputVO.TextForm();
        textForm1.setName("姓名");
        textForm1.setValue("1");
        ProcessInstanceInputVO.TextForm textForm2 = new ProcessInstanceInputVO.TextForm();
        textForm2.setName("性别");
        textForm2.setValue("2");
        ProcessInstanceInputVO.TextForm textForm3 = new ProcessInstanceInputVO.TextForm();
        textForm3.setName("出生年月");
        textForm3.setValue("3");

        ProcessInstanceInputVO.TextForm textForm4 = new ProcessInstanceInputVO.TextForm();
        textForm4.setName("照片");
        textForm4.setValue("[\"https://ae01.alicdn.com/kf/HTB1iww9aEGF3KVjSZFo762mpFXat.png\"]");
       ProcessInstanceInputVO.TextForm textForm5 = new ProcessInstanceInputVO.TextForm();
        textForm5.setName("毕业院校");
        textForm5.setValue("5");

        ProcessInstanceInputVO.TextForm textForm6 = new ProcessInstanceInputVO.TextForm();
        textForm6.setName("所学专业");
        textForm6.setValue("6");

        ProcessInstanceInputVO.TextForm textForm7 = new ProcessInstanceInputVO.TextForm();
        textForm7.setName("毕业时间");
        textForm7.setValue("7");

        ProcessInstanceInputVO.TextForm textForm8 = new ProcessInstanceInputVO.TextForm();
        textForm8.setName("学历");
        textForm8.setValue("8");
        ProcessInstanceInputVO.TextForm textForm9 = new ProcessInstanceInputVO.TextForm();
        textForm9.setName("学位");
        textForm9.setValue("9");

        ProcessInstanceInputVO.TextForm textForm10 = new ProcessInstanceInputVO.TextForm();
        textForm10.setName("工作单位");
        textForm10.setValue("10");

        ProcessInstanceInputVO.TextForm textForm11 = new ProcessInstanceInputVO.TextForm();
        textForm11.setName("通讯地址");
        textForm11.setValue("11");
        ProcessInstanceInputVO.TextForm textForm12 = new ProcessInstanceInputVO.TextForm();
        textForm12.setName("申报类别");
        textForm12.setValue("12");
        ProcessInstanceInputVO.TextForm textForm13 = new ProcessInstanceInputVO.TextForm();
        textForm13.setName("职务");
        textForm13.setValue("13");
        ProcessInstanceInputVO.TextForm textForm14 = new ProcessInstanceInputVO.TextForm();
        textForm14.setName("参加工作时间");
        textForm14.setValue("14");
        ProcessInstanceInputVO.TextForm textForm15 = new ProcessInstanceInputVO.TextForm();
        textForm15.setName("技术职称");
        textForm15.setValue("15");
        ProcessInstanceInputVO.TextForm textForm16 = new ProcessInstanceInputVO.TextForm();
        textForm16.setName("从事专业");
        textForm16.setValue("16");
        ProcessInstanceInputVO.TextForm textForm17 = new ProcessInstanceInputVO.TextForm();
        textForm17.setName("从事专业时间");
        textForm17.setValue("17");
        ProcessInstanceInputVO.TextForm textForm18 = new ProcessInstanceInputVO.TextForm();
        textForm18.setName("身份证号");
        textForm18.setValue("18");
        ProcessInstanceInputVO.TextForm textForm19 = new ProcessInstanceInputVO.TextForm();
        textForm19.setName("家庭地址");
        textForm19.setValue("19");
        ProcessInstanceInputVO.TextForm textForm20 = new ProcessInstanceInputVO.TextForm();
        textForm20.setName("健康状态");
        textForm20.setValue("20");
        ProcessInstanceInputVO.TextForm textForm21 = new ProcessInstanceInputVO.TextForm();
        textForm21.setName("办公室联系方式");
        textForm21.setValue("21");
        ProcessInstanceInputVO.TextForm textForm22 = new ProcessInstanceInputVO.TextForm();
        textForm22.setName("手机联系方式");
        textForm22.setValue("22");
        ProcessInstanceInputVO.TextForm textForm23 = new ProcessInstanceInputVO.TextForm();
        textForm23.setName("本人支持或参加过的典型项目");
        textForm23.setValue("23");

        ProcessInstanceInputVO.TextForm textForm24 = new ProcessInstanceInputVO.TextForm();
        textForm24.setName("业务主管部门");
        textForm24.setValue("24");
        ProcessInstanceInputVO.TextForm textForm25 = new ProcessInstanceInputVO.TextForm();
        textForm25.setName("行政审批局意见");
        textForm25.setValue("25");
        ArrayList<ProcessInstanceInputVO.TextForm> textForms = new ArrayList<>();
        textForms.add(textForm1);
        textForms.add(textForm2);
        textForms.add(textForm3);
        textForms.add(textForm4);
        textForms.add(textForm5);
        textForms.add(textForm6);
        textForms.add(textForm7);
        textForms.add(textForm8);
        textForms.add(textForm9);
        textForms.add(textForm10);
        textForms.add(textForm11);
        textForms.add(textForm12);
        textForms.add(textForm13);
        textForms.add(textForm14);
        textForms.add(textForm15);
        textForms.add(textForm16);
        textForms.add(textForm17);
        textForms.add(textForm18);
        textForms.add(textForm19);
        textForms.add(textForm20);
        textForms.add(textForm21);
        textForms.add(textForm22);
        textForms.add(textForm23);
        textForms.add(textForm24);
        textForms.add(textForm25);
        processInstance.setTextForms(textForms);
        DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_PROCESSINSTANCE_START);
        OapiProcessinstanceCreateRequest request = new OverrideCheck();
        request.setProcessCode(Constant.PROCESS_CODE);

        request.setFormComponentValues(processInstance.generateForms());

        request.setOriginatorUserId(processInstance.getOriginatorUserId());
        request.setDeptId(processInstance.getDeptId());

        OapiProcessinstanceCreateResponse response = client.execute(request, AccessTokenUtil.getToken());
        System.out.println(response);
    }

    /**
     *
     * 注册钉钉回调
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        CallbackController.registerCallbackForDD();
    }
    @Test
    public void testtoken(){
        System.out.println(AccessTokenUtil.getToken());
    }
    @Test
    public  void testjson(){
        String x = "[{\"task_status\":\"COMPLETED\",\"create_time\":\"2020-07-08 17:33:08\",\"task_result\":\"REFUSE\",\"userid\":\"202134036029323796\",\"finish_time\":\"2020-07-08 17:33:51\",\"taskid\":\"64722454197\",\"url\":\"?procInsId=99b0bab5-4941-4963-9d55-09943e32a032&taskId=64722454197&businessId=202007081733000072592\"}]";
        JSONArray array = JSON.parseArray(x);
        System.out.println( array.get(0).toString());
        JSON.parseObject(array.get(0).toString());
    }
    //测试发送消息给专家
    @Test
    public void testsendmessage() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
        req.setUseridList("010953225306-1540554866");
        req.setAgentId(784164874L);
        req.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent("王月鸟真帅");
        req.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, AccessTokenUtil.getToken());
    }

    @Test
    public void testToken(){
        String token = AccessTokenUtil.getToken();
        System.out.println(token);
    }
    @Test
    public void testSaveDingUser(){
        SmartStudentTeacherInfomation s = new SmartStudentTeacherInfomation();
        s.setId("100");
        s.setCreateBy("");
        s.setCreateTime(null);
        s.setUpdateTime(null);
        s.setSysOrgCode(null);
        s.setStudentName("假人");
        s.setStudentStudentNumber("18205611");
        s.setStudentSex("男");
        s.setStudentDempartment("计算机工程系");
        s.setStudentMajor("软件工程");
        s.setStudentTimeEnrollment("2018");
        s.setStudentPhoneNumber("12345678900");
        s.setStudentStanbdy(null);
        s.setStudentStanbdyStanbdy(null);
        smartStudentTeacherInfomationMapper.insertInfomatiron(s);

//        for(int i=0;i<100;i++){
//            SmartStudentTeacherInfomation s = new SmartStudentTeacherInfomation();
//            s.setId("100"+i);
//            s.setCreateBy("");
//            s.setCreateTime(null);
//            s.setUpdateTime(null);
//            s.setSysOrgCode(null);
//            s.setStudentName("假人"+i);
//            s.setStudentStudentNumber("18205611"+i);
//            s.setStudentSex("男");
//            s.setStudentDempartment("计算机工程系");
//            s.setStudentMajor("软件工程");
//            s.setStudentTimeEnrollment("2018");
//            s.setStudentPhoneNumber("12345678900");
//            s.setStudentStanbdy(null);
//            s.setStudentStanbdyStanbdy(null);
//            smartStudentTeacherInfomationMapper.insertInfomatiron(s);
//        }
    }
}
